//
//  KDPatientDataViewController.m
//  LaiKeDaoClient
//
//  Created by dongkedi的iMac on 15/11/11.
//  Copyright © 2015年 LKD. All rights reserved.
//

#import "KDPatientDataViewController.h"
#import "KDMypatientTableViewCell.h"
#import "KDPatientInforViewController.h"
#import "KDEdieTagViewController.h"
@interface KDPatientDataViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate,UIWebViewDelegate>{
    UITableView                 *_tableView;
    UIWebView                   *_webView;
}


@end

@implementation KDPatientDataViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    [self setNaviTitle:@"患者资料"];
    [self setNaviRightButton:@"编辑"];
    [self setNaviLeftButton];
    
    _tableView = [[UITableView alloc] initWithFrame:(CGRect){0,0,kScreenWidth,54+20} style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate   = self;
    _tableView.scrollEnabled = NO;
    _tableView.backgroundColor = [UIColor colorWithHexString:@"0xF2F2F2"];
    [_tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.view addSubview:_tableView];
    
    UILabel     *label1= [[UILabel alloc] initWithFrame:(CGRect){0,HEIGHT(_tableView),kScreenWidth,22}];
    [label1 setTextAlignment:NSTextAlignmentCenter];
    [label1 setFont:[UIFont systemFontOfSize:12]];
    [label1 setBackgroundColor:[UIColor whiteColor]];
    [label1 setTextColor:[UIColor lightGrayColor]];
    [label1 setText:@"就诊记录(h5)"];
    [self.view addSubview:label1];

    _webView = [[UIWebView alloc] initWithFrame:(CGRect){0,Y(label1)+HEIGHT(label1),self.view.bounds.size.width,self.view.bounds.size.height-64-HEIGHT(_tableView)-HEIGHT(label1)}];
    _webView.scalesPageToFit =YES;
    _webView.delegate =self;
    _webView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:_webView];
    
    _menuView = [[KDMenuView alloc] initWithFrame:self.view.frame];
    [self.view addSubview:_menuView];
    _menuView.hidden = YES;
    _menuView.menuDelegate = self;
    
    _BlackSheetView = [[KDBlackSheetView alloc] initWithFrame:self.view.frame];
    _BlackSheetView.blackSheetDelegate = self;
}

- (void)viewWillAppear:(BOOL)animated
{
    SharedAppDelegate.tabBarController.tabBarIsHiden = YES;
    _menuView.hidden = YES;
    if (self.dataDic.patient_code) {
        [self requestGetMedicalRecordPatient_code:self.dataDic.patient_code];
    }
}

- (void)viewWillDisappear:(BOOL)animated
{
    SharedAppDelegate.tabBarController.tabBarIsHiden = NO;
    _menuView.hidden = YES;
}

- (void)clickRightButton:(id)sender {
    _menuView.hidden = !_menuView.hidden;
}

//网络交互
//getMedicalRecordPatient_code
- (void)requestGetMedicalRecordPatient_code:(NSString *)Patient_code
{
    [CSHApiClient getMedicalRecordPatient_code:Patient_code didFinishLoaded:^(KXJson *json, NSString *path) {
        //
        NSDictionary *dic1 = [NSDictionary dictionaryWithDictionary:[json getObjectForKey:@"data"]];
        NSLog(@"url = %@",dic1[@"url"]);
        if ([dic1[@"url"] rangeOfString:@"xxx"].location !=NSNotFound) {
            [self loadWebPageWithString:@"http://www.baidu.com"];
        }else{
            [self loadWebPageWithString:dic1[@"url"]];
        }
        
    } didFailLoaded:^(NSError *error, NSString *path) {
        //
    }];
}

//setToBlackListPatient_code
- (void)requestSetToBlackListPatient_code:(NSString *)Patient_code
{
    [CSHApiClient setToBlackListPatient_code:Patient_code didFinishLoaded:^(KXJson *json, NSString *path) {
        //
        if ([[json getStringForKey:@"code"] intValue] == 200) {
            [self showAlert:[json getStringForKey:@"message"]];
        }else{
            [self showAlert:@"拉黑失败,请重试"];
        }
    } didFailLoaded:^(NSError *error, NSString *path) {
        //
    }];
}

- (void)showAlert:(NSString *)str
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:str delegate:nil cancelButtonTitle:@"确定" otherButtonTitles: nil];
    [alert show];
}

//menu delegate
- (void)chooseButton:(NSInteger)btnTag
{
    _menuView.hidden = YES;
    //101,编辑,102,黑名单,103,取消
    switch (btnTag) {
        case 101:
            [self.navigationController pushViewController:[[KDEdieTagViewController alloc] init] animated:YES];

            break;
        case 102:
            [self.view addSubview:_BlackSheetView];
            break;
        case 103:
            
            break;
        default:
            break;
    }
}
//blacksheet delegate
- (void)chooseSheet:(NSInteger)btnTag
{
    [_BlackSheetView removeFromSuperview];
    //101,确定
    if (btnTag == 101) {
        //
        if (self.dataDic.patient_code) {
            [self requestSetToBlackListPatient_code:self.dataDic.patient_code];
        }
    }
}

#pragma mark - UIWebViewDelegate
- (void)loadWebPageWithString:(NSString*)urlString
{
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString]        cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];
    [request setHTTPMethod:@"GET"];
    
    [_webView loadRequest:request];
}

- (void)webViewDidStartLoad:(UIWebView *)webView//开始加载的时候执行该方法。
{
}

- (void)webViewDidFinishLoad:(UIWebView *)webView//加载完成的时候执行该方法。
{
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error//加载出错的时候执行该方法。
{
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section;
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 54;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    KDPatientInforViewController *controller = [[KDPatientInforViewController alloc] init];
    controller.dataDic = self.dataDic;
    [self.navigationController pushViewController:controller animated:YES];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString* str=  @"cell";
    KDMypatientTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier: str];
    if (cell ==nil) {
        cell = [[KDMypatientTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:str];
        
        UIView  *line = [[UIView alloc] initWithFrame:(CGRect){0,53,WIDTH(cell.contentView),1}];
        [line setBackgroundColor:[UIColor colorWithHexString:@"0xF2F2F2"]];
        [cell.contentView addSubview:line];
    }
    if (self.dataDic) {
        [cell reloadData:self.dataDic];
    }
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
